﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.WinForms;
using MINERP.BLL.PhotoRindGrid;
using MINERP.COM;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.OverOrder;
using MINERP.NEWPhotoRindGrid.IView;
using MINERP.NEWPhotoRindGrid.Presenter;
using MINERP.MODEL.NewPhotoRindGrid;
using MINERP.NEWPhotoRindGrid.Forms;
using MINERP.PhotoRindGrid.Forms;

namespace MINERP.NEWPhotoRindGrid
{
    public partial class FrmMain : FrmParent, IMainView
    {
        private bool _IsAutoAssignShop = false;
        private MainPresenter _prenster;
        private RasterThumbnailBrowser _imgBrowSer = new RasterThumbnailBrowser();


        public FrmMain()
        {
            InitializeComponent();
            base.InitiAuthority();
            splitContainer1.Panel2Collapsed = true;

            //_imgBrowSer = new FlowLayoutPanel();
            _imgBrowSer.Width = gridControlPhoto.Width;
            _imgBrowSer.Height = gridControlPhoto.Height;
            _imgBrowSer.Location = gridControlPhoto.Location;
            _imgBrowSer.Anchor = gridControlPhoto.Anchor;
      
            _imgBrowSer.BorderStyle = BorderStyle.FixedSingle;
            splitContainer1.Panel2.Controls.Add(_imgBrowSer);

            _imgBrowSer.Visible = false;
        }

        #region IMainView 成员

        public void ShowOrderDetail(MyDefineGrid myGrid)
        {
            this.Cursor = Cursors.WaitCursor;
            if (splitContainer1.Panel2Collapsed)
            {
                splitContainer1.Panel2Collapsed = false;
            }
            progressBar1.Visible = false;
            gridControlPhoto.Visible = true;
            _imgBrowSer.Visible = false;
            btnLookImg.Text = "查看缩略图";

            gridControlPhoto.DataSource = myGrid.gridInfo.OrderInfo.PhotoS;
            gridControlPhoto.RefreshDataSource();

            lblOrderProductId.Text = "产品号：" + myGrid.gridInfo.OrderInfo.OrderProductId;
            lblCtmName.Text = "客户：" + myGrid.gridInfo.OrderInfo.CtmName;
            lblGridName.Text = "格子名：" + myGrid.gridInfo.GridBaseInfo.GridName;
            lblOrderProductName.Text = "产品名：" + myGrid.gridInfo.OrderInfo.OrderProductName;
            lblPCount.Text = "总数量：" + myGrid.gridInfo.OrderInfo.PhotoCount.ToString();
            lblPInCount.Text = "放入数量：" + myGrid.gridInfo.OrderInfo.InGridCount.ToString();
            lblShopName.Text = "商户名：" + myGrid.gridInfo.OrderInfo.ShopName;
            this.Cursor = Cursors.Default;
        }

        public bool setClearBtnEnable
        {
            set { btnClearBox.Enabled = value; }
        }

        public bool setPrintBtnEnable
        {
            set { btnPrint.Enabled = value; }
        }

        public bool setSelectedClearBoxEnable
        {
            set { tsbAdvanceProduct.Enabled = value; }
        }


        public List<UCGrid> CurSelectedGridS
        {
            get { return null; }
        }

        public string InpuPhotoCode
        {
            get { return _curBarCode; }
        }

        public void DisPlayGrid(List<UCGrid> UgridS)
        {
            SetLayOutPanel();
            foreach (var uc in UgridS)
            {
                tableLayoutPanelGrid.Controls.Add(uc);
            }
        }

        public void UpdateGrid(UCGrid grid)
        {
            throw new NotImplementedException();
        }

        public void DisPlayCurGridNameInLbl(string strGridName)
        {
            throw new NotImplementedException();
        }

        public void ClearInputTxtBox()
        {
            throw new NotImplementedException();
        }

        public void DisPlayBaseMsg(string strMsg)
        {
            throw new NotImplementedException();
        }

        public void ShowFandan(List<PhotoInfo_Info> list)
        {
            if (list.Count > 0)
            {
                FrmFanDan frmFanDan = new FrmFanDan(list, base.userId, base.CurrentTechID);
                if (frmFanDan.ShowDialog() == DialogResult.OK)
                {
                    list.ForEach(p =>
                    {
                        if (p.Status == 3)
                        {
                            p.Status = 1;
                        }
                    });
                }
            }

        }

        public Size GetDisplaySize
        {
            get { return Screen.GetWorkingArea(this.splitContainer1).Size; }
        }

        delegate void showImagDelegate(List<RasterImageListItem> listImg);
        public void ShowImg(List<RasterImageListItem> listImg)
        {
            if (_imgBrowSer.InvokeRequired)
            {
                _imgBrowSer.Invoke(new showImagDelegate(ShowImg), new object[] { listImg });
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            if (_imgBrowSer.Items.Count > 0)
            {
                _imgBrowSer.Items.Clear();
            }
            foreach (var item in listImg)
            {
                //_imgBrowSer.Controls.Add(new RasterImageViewer() { Image = item.Image, SizeMode = Leadtools.RasterPaintSizeMode.Stretch });
                _imgBrowSer.Items.Add(item);
            }
            this.Cursor = Cursors.Default;
        }

        public void SetProcessGo()
        {
            if (!progressBar1.Visible)
            {
                progressBar1.Visible = true;
            }

            if (progressBar1.Value >= progressBar1.Maximum)
            {
                progressBar1.Value = 0;
            }
            progressBar1.Value++;
        }


        public void SetProcessOver()
        {
            progressBar1.Value = 0;
            progressBar1.Visible = false;
        }

        const int GRIDNAMECOUNT = 4;//最多显示4个格子
        int curGridCount = 0;
        StringBuilder strGridNameList = new StringBuilder();
        delegate void ShowGridNameDelegate(string gridName);
        public void ShowGridName(string gridName)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ShowGridNameDelegate(ShowGridName), new string[] { gridName });
                return;
            }
            curGridCount++;
            if (curGridCount <= GRIDNAMECOUNT)
            {
                strGridNameList.Append(gridName);
            }
            else
            {
                char firstChar = gridName.ToCharArray()[0];
                int index = strGridNameList.ToString().IndexOf(firstChar, 1);
                if (index > 0)
                {
                    strGridNameList.Remove(0, index);
                    strGridNameList.Append(gridName);
                    curGridCount--;
                }
            }

            lblShowGridName.Text = strGridNameList.ToString();
        }


        public void CloseDetailPanel()
        {
            if (!splitContainer1.Panel2Collapsed)
            {
                splitContainer1.Panel2Collapsed = true;
            }
        }

        public bool SetEnterTxtEnabled
        {
            set { txtBoxPhotoBarCode.Enabled = value; }
        }

        #endregion


        private void SetLayOutPanel()
        {
            tableLayoutPanelGrid.ColumnStyles.Clear();
            tableLayoutPanelGrid.RowStyles.Clear();
            tableLayoutPanelGrid.Padding = new Padding(0);
            tableLayoutPanelGrid.RowCount = DataTransfer.CurSubArea.WidthNum;
            tableLayoutPanelGrid.ColumnCount = DataTransfer.CurSubArea.HeightNum;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            chkAutoAssign.Checked = Properties.Settings.Default.IsAutoAssignShop;
            DataTransfer.OpUid = base.userId;
            // DataTransfer.OpUid = 7;
            lblCurrentUser.Text = base.userName;
            DataTransfer.curTechId = base.CurrentTechID;
            _prenster = new MainPresenter(this);
            _IsAutoAssignShop = _prenster.IsAutoAssignShop();

        }

        private string _curBarCode = string.Empty;

        private void toolStripButton3_Click(object sender, EventArgs e)//确定分配格子
        {
            btnOK.Enabled = false;
            _curBarCode = txtBoxPhotoBarCode.Text.Trim();
            txtBoxPhotoBarCode.Clear();
            _prenster.AssignGrid();
            btnOK.Enabled = true;
        }

        private StringBuilder strBPhotoBarCode = new StringBuilder();
        private void FrmMain_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtBoxPhotoBarCode.Focused)
            {
                if (char.IsDigit(e.KeyChar))
                {
                    if (strBPhotoBarCode.Length >= 10)
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        strBPhotoBarCode.Append(e.KeyChar);
                    }

                    txtBoxPhotoBarCode.Text = strBPhotoBarCode.ToString();
                }
            }


            if (e.KeyChar == (int)Keys.Enter)
            {
                if (txtBoxPhotoBarCode.Text.Length > 0)
                {
                    btnOK.PerformClick();
                    strBPhotoBarCode = new StringBuilder();
                }
            }
            else if (e.KeyChar == (int)Keys.Escape)
            {
                if (!splitContainer1.Panel2Collapsed)
                {
                    splitContainer1.Panel2Collapsed = true;
                }
            }
        }



        private void btnLookImg_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            //加载缩略图
            if (btnLookImg.Text == "查看缩略图")
            {
                btnLookImg.Enabled = false;
                _imgBrowSer.Visible = true;
                gridControlPhoto.Visible = false;

                _prenster.GetImageList();
                btnLookImg.Text = "查看列表";
                btnLookImg.Enabled = true;
            }
            else if (btnLookImg.Text == "查看列表")
            {
                btnLookImg.Enabled = false;
                _imgBrowSer.Controls.Clear();
                _imgBrowSer.Visible = false;
                gridControlPhoto.Visible = true;
                btnLookImg.Text = "查看缩略图";
                btnLookImg.Enabled = true;
            }
            this.Cursor = Cursors.Default;
        }

        private void btnClearBox_Click(object sender, EventArgs e)
        {
            //清空选中的格子
            this.Cursor = Cursors.WaitCursor;
            _prenster.ClearSelectedGrid();
            this.Cursor = Cursors.Default;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //强制打印选中的格子
            this.Cursor = Cursors.WaitCursor;
            _prenster.ForcePrint();
            this.Cursor = Cursors.Default;
        }

        private void tsbAdvanceProduct_Click(object sender, EventArgs e)
        {
            //清柜
            _prenster.PrioritySelectedGird();
        }

        private void tsbAdvance_Click(object sender, EventArgs e)
        {
            //条件清柜
            _prenster.ConditionPriorityGrid();
        }

        private void tsbtnLogout_Click(object sender, EventArgs e)
        {
            //刷新格子
            this.Cursor = Cursors.WaitCursor;
            tsbtnLogout.Enabled = false;
            _prenster.RefreshData();
            this.Cursor = Cursors.Default;
            tsbtnLogout.Enabled = true;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //复检功能
            FrmReCheck frmCheck = new FrmReCheck();
            frmCheck.ShowDialog();
        }

        private void gridViewphoto_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                PhotoInfo_Info cruPi = (PhotoInfo_Info)gridViewphoto.GetRow(e.RowHandle);
                if (cruPi.Status == 4)
                {
                    if (cruPi.IsRebackPhoto)
                    {
                        e.Appearance.BackColor = Color.Green;//返单照片并且已进格子的用绿色表示
                    }
                    else
                    {
                        e.Appearance.BackColor = Color.GreenYellow;
                    }

                }
                else
                {
                    if (cruPi.IsRebackPhoto)
                    {
                        e.Appearance.BackColor = Color.Blue;//返单的照片并且未进格子，用红蓝色标识
                    }
                }
            }
        }

        private void gridViewphoto_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.Name == "FlowStatus")
                {
                    switch (((PhotoInfo_Info)gridViewphoto.GetRow(e.RowHandle)).Status)
                    {
                        case 1: e.DisplayText = "拼板中";
                            break;
                        case 0: e.DisplayText = "分配打印机";
                            break;
                        case 2: e.DisplayText = "打印中";
                            break;
                        case 3: e.DisplayText = "打印完成";
                            break;
                        case 4: e.DisplayText = "格子中";
                            break;
                    }
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //退出
            this.Close();
        }

        private void buttonCreateFan_Click(object sender, EventArgs e)
        {
            //
            _prenster.ReBackOrder();
        }

        private void tsBtn_Seach_Click(object sender, EventArgs e)
        {
            //按商户查询
            _prenster.SearchByShopName();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmMain frm = ComControls.frmMain.GetInstance(DataTransfer.OpUid);
            frm.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //60刷新一下界面
            tsbtnLogout_Click(null, null);
        }


        #region IMainView 成员


        public bool isShowPrintDialog
        {
            get { return CkIsShowPrintDialog.Checked; }
        }

        public bool IsAutoAssign
        {
            get { return _IsAutoAssignShop; }
        }

        #endregion

        private void CkIsShowPrintDialog_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void chkAutoAssign_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.IsAutoAssignShop = chkAutoAssign.Checked;
            Properties.Settings.Default.Save();
        }

        private void checkBoxIsPrint_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.IsPrintTicket = checkBoxIsPrint.Checked;
            Properties.Settings.Default.Save();
        }
    }
}
